import React from 'react';
import { Modal } from 'antd';
import { INFO_TYPE, MODAL_DEFAULT_PARAM, MODAL_FORCE_PARAM } from './config';
import { getCommonContent } from './commonContent';
import { getTableContent } from './tableContent';
import { formatMessage } from 'umi';

const show = config => {
    const { dataSource, columns, type = INFO_TYPE.COMMON, ...rest } = config;
    if (!dataSource || !columns) {
        return Modal.info(config);
    }
    // Modal参数处理
    const modalParams = { ...MODAL_DEFAULT_PARAM, ...rest, ...MODAL_FORCE_PARAM };
    let content = null;
    switch (type) {
        case INFO_TYPE.COMMON:
            content = getCommonContent(dataSource, columns);
            break;
        case INFO_TYPE.TABLE:
            content = getTableContent(dataSource, columns);
            break;
        default:
            break;
    }
    return Modal.info({ ...modalParams, content, okText: <div>{formatMessage({ id: 'btn.form.confirm' })}</div> });
};
const withCommon = config => ({ ...config, type: INFO_TYPE.COMMON });
const withTable = config => ({ ...config, type: INFO_TYPE.TABLE });
const showCommonModal = config => show(withCommon(config));
const showTableModal = config => show(withTable(config));

export default { show, showCommonModal, showTableModal };
